﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using RCSP.Biz.Models;
using RCSP.DataAccess;

namespace RCSP.Biz.Repositories
{
    public interface IPresetRepository
    {
        List<Preset> GetPresetByType(int presetType);

    }

    public class PresetRepository : IPresetRepository
    {
        public List<Preset> GetPresetByType(int presetType)
        {
            var presets = new List<Preset>();
            var presetDB = new PresetDataAccess();
            var ds = presetDB.GetPresetByType(presetType);

            if (ds == null || ds.Tables.Count <= 0)
            {
                return presets;
            }

            DataTable dt = ds.Tables[0];

            return ConvertToPreset(dt);
        }

        private List<Preset> ConvertToPreset(DataTable dt)
        {
            var presets = new List<Preset>();

            if (dt == null)
            {
                return presets;
            }

            presets.AddRange(from DataRow row in dt.Rows
                select new Preset
                {
                    Code = row["CODE"] != null ? int.Parse(row["CODE"].ToString()) : -1,
                    Name = row["NAME"] != null ? row["NAME"].ToString() : string.Empty,
                    NameX = row["NAMEX"] != null ? row["NAMEX"].ToString() : string.Empty,
                    PresetType = row["PRSET_TYPE"] != null ? int.Parse(row["PRSET_TYPE"].ToString()) : -1
                });

            return presets;
        }

    }
}
